package org.example.wu_and_dian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.wu_and_dian.config.UserContext;
import org.example.wu_and_dian.constant.HttpStatus;
import org.example.wu_and_dian.entity.User;
import org.example.wu_and_dian.exception.LeaseException;
import org.example.wu_and_dian.mapper.UserMapper;
import org.example.wu_and_dian.service.LoginService;
import org.example.wu_and_dian.utils.PasswordUtil;
import org.example.wu_and_dian.utils.StringUtils;
import org.example.wu_and_dian.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static org.example.wu_and_dian.utils.TokenUtils.createToken;

@Service
public class LoginServiceImpl extends ServiceImpl<UserMapper, User> implements LoginService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public String login(String email, String password) {

        User user = userMapper.selectUserByEmail(email);
        if (user == null || !user.getPassword().equals(password)) {
            throw new RuntimeException("邮箱或密码错误");
        }


        // 可以同时设置ThreadLocal，但仅用于当前请求
        UserContext.set(user.getEmail(), user.getPassword());
        return TokenUtils.createToken(user.getEmail(),user.getPassword());
    }
}
